//
//  HomeView.m
//  QinrenManagers
//
//  Created by lemon on 15/5/22.
//  Copyright (c) 2015年 easaa. All rights reserved.
//

#import "HomeView.h"


@interface HomeView ()
{
    //*活动通知父视图
    UIImageView * _headerView;
    
    //按钮父视图
    UIView * _buttonBackView;
    
    //营销策划父视图
    UIView * _bottomView;
    
    NSArray * _buttonTitleArr;
    NSArray * _buttonImageArr;
    
    
    NSMutableArray *policyResultArry;
    
}



@end

@implementation HomeView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        
        _policyModelArry = [NSMutableArray array];
        policyResultArry = [NSMutableArray array];
       
        [self creatData];

       // [self createButtonWithAttributes:_buttonTitleArr];

       
        [self requestForBottomData];
        [self createButtonWithAttributes:_buttonTitleArr];
        
        self.backgroundColor = [UIColor whiteColor];
    
    }
    
    return self;
}

- (void)requestForBottomData {
    
    EAHttpAPIClient *manager = [EAHttpAPIClient manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *parameters = @{@"classid":[NSString stringWithFormat:@"%d",3],@"pagesize":@"10",@"pageindex":[NSString stringWithFormat:@"%d",1]/**,(long)_comboxSelectedIndex]*/};
    NSLog(@"%@",parameters);
    [manager GetWithParameters:parameters method:@"search.article.list" success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *resposeString ;
        if ([responseObject isKindOfClass:[NSData class]]) {
            resposeString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        }else
            resposeString = responseObject;
        
        NSString *jsonString = [DataParser parseString:resposeString];
        id dictionary = [JSONKit jsonToArrayOrDictionary:jsonString];
        NSLog(@"营销政策的dictonary :%@",[dictionary description]);
        
       
        if ([[dictionary objectForKey:@"verification"]intValue] == 1)
        {
            
            for (NSDictionary *dataDic in dictionary[@"data"]) {
                
                PolicyModel *policyModel = [[PolicyModel alloc]init];
                [policyModel setValuesForKeysWithDictionary:dataDic];
                [_policyModelArry addObject:policyModel];
                [policyResultArry addObject:policyModel.name];
            }
            
//            _policyString = [policyResultArry componentsJoinedByString:@"\n"];
            [self creatUI];
            
        }
        else
        {
            NSString *error = [dictionary objectForKey:@"error"];
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
            [SVProgressHUD showErrorWithStatus:error];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
    }];
    
}


- (void)creatData
{
    User *user = [User sharedUser];
    
    if (user.employee_type == 1) {
        _buttonTitleArr = @[@"健康管家群",@"工作群",@"客户方案",@"服务预约",@"我的个人业绩",@"我的团队业绩"];
    }
    else if (user.employee_type == 2) {
        _buttonTitleArr = @[@"管理师群",@"工作群",@"客户方案",@"服务预约",@"我的个人业绩",@"我的团队业绩"];
    }
    
    _buttonImageArr = @[@"guanjia_",@"gongzuoqun_",@"kehufangan_",@"fuwuyuyue_",@"gerenyeji_",@"tuanduiyeji_"];
}

- (void)creatUI
{
    //加入按钮
    
//    UILabel * scoreLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_buttonBackView.frame)-85, CGRectGetMaxY(_buttonBackView.frame)-20, 80, 20)];
//
//    scoreLabel.text = @"积分  56";
//    scoreLabel.font = [UIFont fontWithName:@"Arial" size:12];
//    scoreLabel.textColor = [UIColor blueColor];
//    [self addSubview:scoreLabel];
    
    _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_buttonBackView.frame)+3, SCREEN_W, (SCREEN_H-49-64)/3+1)];
    
    _bottomView.backgroundColor = [UIColor whiteColor];
    //营销策划
    UILabel * headlineLable = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 80, 20)];
    
    headlineLable.text = @"营销策略";

//    //策划标题
//    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(headlineLable.frame)+10, _bottomView.frame.size.width, 20)];
//    //titleLabel.backgroundColor = [UIColor redColor];
//    
//    titleLabel.text = @"";
//    titleLabel.font = [UIFont fontWithName:@"Arial" size:15.0f];
    
    //更多按钮
    UIButton * moreButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_W - 100, CGRectGetMinY(headlineLable.frame), 70, 15)];
    
    [moreButton setBackgroundImage:[UIImage imageNamed:@"chakanxiangqing_"] forState:UIControlStateNormal];
    
    moreButton.tag = 1006;
    [moreButton addTarget:self action:@selector(onClikedButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    //策划内容
//    UILabel * contentsLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(headlineLable.frame)+10 , _bottomView.frame.size.width, _bottomView.frame.size.height - headlineLable.frame.size.height - titleLabel.frame.size.height - 10)];
//    
//   // contentsLabel.backgroundColor = [UIColor greenColor];
//    _policyString = [_policyString stringByReplacingOccurrencesOfString:@"+" withString:@""];
//    NSString * content = _policyString;
//    //contentsLabel.text = content;
//    contentsLabel.textColor = [UIColor blackColor];
//    
//    UIFont * textFont = [UIFont systemFontOfSize:13];
//
//    contentsLabel.font = textFont;
//    contentsLabel.numberOfLines = 0;
//    
//    CGSize size = [content sizeWithFont:textFont constrainedToSize:CGSizeMake(SCREEN_W - 40, 400.0f) lineBreakMode:UILineBreakModeWordWrap];
//    CGRect rect = contentsLabel.frame;
//    rect.size = size;
//    [contentsLabel setFrame:rect];
//    [contentsLabel setText:content];
    
    //[bottomImage setImage:[UIImage imageNamed:@"1.jpg"]];
    
    if (_policyModelArry.count>3) {
        for (NSInteger i = 0; i < 3; ++i) {
            UIButton *btn  = [[UIButton alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(headlineLable.frame)+10+30*i, SCREEN_W-20, 20)];
            
            [btn addTarget:self action:@selector(onClikedButton:) forControlEvents:UIControlEventTouchUpInside];
            
            btn.tag = 2000+i;
            btn.backgroundColor = [UIColor clearColor];
            [_bottomView addSubview:btn];
            
            UILabel *btnLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(headlineLable.frame)+10+30*i, SCREEN_W-20, 20)];
            
            btnLabel.textAlignment = NSTextAlignmentLeft;
            btnLabel.text = [policyResultArry[i] stringByReplacingOccurrencesOfString:@"+" withString:@""];
            btnLabel.textColor = [UIColor blackColor];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:12]];
            btn.backgroundColor = [UIColor clearColor];
            [_bottomView addSubview:btnLabel];

        }
    }else {
        
        for (NSInteger i = 0; i < _policyModelArry.count; ++i) {
            UIButton *btn  = [[UIButton alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(headlineLable.frame)+10+30*i, SCREEN_W-20, 20)];
            
            [btn addTarget:self action:@selector(onClikedButton:) forControlEvents:UIControlEventTouchUpInside];
            
            btn.tag = 2000+i;
            btn.backgroundColor = [UIColor clearColor];
            [_bottomView addSubview:btn];
            
            UILabel *btnLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(headlineLable.frame)+10+30*i, SCREEN_W-20, 20)];
            
            btnLabel.textAlignment = NSTextAlignmentLeft;
            btnLabel.text = [policyResultArry[i] stringByReplacingOccurrencesOfString:@"+" withString:@""];
            btnLabel.textColor = [UIColor grayColor];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:12]];
            btn.backgroundColor = [UIColor clearColor];
            [_bottomView addSubview:btnLabel];
            
        }

    
    
    
    }
    
    [_bottomView addSubview:headlineLable];
//    [_bottomView addSubview:titleLabel];
//    [_bottomView addSubview:contentsLabel];
    [_bottomView addSubview:moreButton];
    
    
    [self addSubview:_bottomView];
    
}


- (void)createButtonWithAttributes:(NSArray *)att
{
    _headerView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, SCREEN_W,(SCREEN_H-49-64)/3)];
    
   // [_headerView setImage:[UIImage imageNamed:@"one"]];
    
    _headerView.backgroundColor = Lightlightcolor;
    
//    [self addSubview:_headerView];
    
    _buttonBackView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_headerView.frame),SCREEN_W,(SCREEN_H-49-64)/3-1)];
    _buttonBackView.backgroundColor = [UIColor lightGrayColor];
    for (NSString *name in att) {
        int idx = (int)[att indexOfObject:name];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 1000 + idx;
        
        int originX = idx % 2 ? 1:0;
        int originY = idx / 2 * ((SCREEN_H-49-65)/9);
        //int originY = idx / 2 * ((SCREEN_H-49-64)/9-10) + idx/2*20 + 1;
        
        CGRect aframe = CGRectMake( originX * SCREEN_W / 2, originY , SCREEN_W / 2-0.5, (SCREEN_H-49-65)/9-1);
        
        button.frame = aframe;
        
        button.backgroundColor = [UIColor whiteColor];
        
        [button setTitle:name forState:UIControlStateNormal];
        //button.titleLabel.font = [UIFont systemFontOfSize:16];//title字体大小
        button.titleLabel.textAlignment = NSTextAlignmentLeft;//设置title的字体居中
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//设置title在一般情况下为白色字体
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];//设置title在button被选中情况下为灰色字体
        [button setImage:[UIImage imageNamed:_buttonImageArr[idx]] forState:UIControlStateNormal];//给button添加image
        
//        if(button.tag == 1000)
//        {
//            User *user = [User sharedUser];
//            
//            if (user.employee_type == 2) {
//                button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
//                button.imageEdgeInsets = UIEdgeInsetsMake(0,-70,0,0);
//            }
//            else if (user.employee_type == 1) {
//                button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 50);
//                button.imageEdgeInsets = UIEdgeInsetsMake(0,0,0,0);
//                button.backgroundColor = [UIColor blueColor];
//            }
//        }
         if (button.tag == 1001)
        {
           
            button.titleEdgeInsets = UIEdgeInsetsMake(0, -55, 0, 0);
            button.imageEdgeInsets = UIEdgeInsetsMake(0,-70,0,0);
        }
        else if (button.tag == 1002)
        {
            button.titleEdgeInsets = UIEdgeInsetsMake(0, -50, 0, 0);
            button.imageEdgeInsets = UIEdgeInsetsMake(0,-65,0,0);
        }
//        else if (button.tag == 1003)
//        {
//            button.titleEdgeInsets = UIEdgeInsetsMake(0, -55, 0, 0);
//            button.imageEdgeInsets = UIEdgeInsetsMake(0,-70,0,0);
//        }
        else if (button.tag == 1004)
        {
            button.titleEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
            button.imageEdgeInsets = UIEdgeInsetsMake(0,-30,0,0);
        }
        else if (button.tag == 1005)
        {
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
            button.imageEdgeInsets = UIEdgeInsetsMake(0,-15,0,0);
        }
        else
        {
            button.titleEdgeInsets = UIEdgeInsetsMake(0, -35, 0, 0);//设置title在button上的位置（上top，左left，下bottom，右right）
            button.imageEdgeInsets = UIEdgeInsetsMake(0,-50,0,0);//设置image在button上的位置（上top，左left，下bottom，右right）这里可以写负值，对上写－5，那么image就象上移动5个像素
        }

        [button addTarget:self action:@selector(onClikedButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [_buttonBackView addSubview:button];

    }
    

    [self addSubview:_buttonBackView];
    
}

- (void)onClikedButton:(UIButton *)sender
{
    if (self.sendBlock) {
        self.sendBlock(sender);
    }
}


//-(void)btnClick:(UIButton *)sender
//{
//    // NSLog(@"%d",sender.selected);
//    if (self.sendBlock) {
//        self.sendBlock(sender);
//    }
//}


@end
